angular.module('starter.controllers')
.controller('ArtlistCtrl', function($scope, museumFac, $stateParams, $location, $timeout, $cookies, uploadImg) {
  $scope.width1 = $(window).width();
  $scope.width2 = ($scope.width1-20)/2;
  $scope.width3 = $scope.width2 - 20;

  $scope.cate_id = $stateParams.categoryId;

  $scope.currentPage = 0;

  $scope.checkOpenid = function(){
    $scope.url = window.location.href;
    if($stateParams.categoryId == 11){
      $scope.title1 = "四方当代艺术湖区 | 建筑列表\r\n四方当代美术馆";
      $scope.title2 = "四方当代艺术湖区 | 建筑列表";
    }else{
      $scope.title1 = "曾根裕:黑曜石 | 作品列表\r\n四方当代美术馆";
      $scope.title2 = "曾根裕:黑曜石 | 作品列表";
    }
    wx.ready(function(){
      wx.onMenuShareTimeline({
        title: $scope.title1, // 分享标题
        link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/banner0.jpg', // 分享图标
        success: function () { 
            // 用户确认分享后执行的回调函数
        },
        cancel: function () { 
            // 用户取消分享后执行的回调函数
        }
    });
    // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
    wx.onMenuShareAppMessage({
        title: $scope.title2,  // 分享标题
        desc: '四方当代美术馆', // 分享描述
        link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/banner0.jpg', // 分享图标
        type: '', // 分享类型,music、video或link，不填默认为link
        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
        success: function () { 
            // 用户确认分享后执行的回调函数
        },
        cancel: function () { 
            // 用户取消分享后执行的回调函数
        }
      });
    });
  }

  $scope.getWorks = function() {
    $scope.currentPage ++;

    // var hheight = ($scope.currentPage+1)*600;
    // document.getElementById('box_art').setAttribute('minHeight', hheight+'px');

    museumFac.info.works({
      categoryId:$stateParams.categoryId,
      page:$scope.currentPage
    },function(data){
      // $scope.works = data.info;
      $scope.subscribe = data.subscribe;
      if(data.info){
            $scope.works =$scope.works? $scope.works.concat(data.info) : data.info;
            $scope.nomark1 = 0;

            // var hheight = ($scope.currentPage)*600;
            // document.getElementById('box_art').setAttribute('minHeight', hheight+'px');
            // alert(hheight);
        }else{
            $scope.nomore = 1;
            // alert('已无更多！');
            }
      $scope.$broadcast('scroll.infiniteScrollComplete');      

      $scope.checkOpenid();

      $timeout(function() {
        
        (function ($){
          var $tiles = $('#tiles'),
              $handler = $('li', $tiles),
              $main = $('#main'),
              $window = $(window),
              $document = $(document),
              options = {
                autoResize: true, // This will auto-update the layout when the browser window is resized.
                container: $main, // Optional, used for some extra CSS styling
                offset: 10, // Optional, the distance between grid items
                itemWidth: $scope.width2 // Optional, the width of a grid item
              };

          /**
           * Reinitializes the wookmark handler after all images have loaded
           */
          function applyLayout() {
            $tiles.imagesLoaded(function() {
              // Destroy the old handler
              if ($handler.wookmarkInstance) {
                $handler.wookmarkInstance.clear();
              }

              // Create a new layout handler.
              $handler = $('li', $tiles);
              $handler.wookmark(options);
            });
          }

          /**
           * When scrolled all the way to the bottom, add more tiles
           */
          function onScroll() {
            // Check if we're within 100 pixels of the bottom edge of the broser window.
            var winHeight = window.innerHeight ? window.innerHeight : $window.height(), // iphone fix
                closeToBottom = ($window.scrollTop() + winHeight > $document.height() - 100);

            if (closeToBottom) {
              // Get the first then items from the grid, clone them, and add them to the bottom of the grid
              var $items = $('li', $tiles),
                  $firstTen = $items.slice(0, 10);
              $tiles.append($firstTen.clone());

              applyLayout();
            }
          };

          // Call the layout function for the first time
          applyLayout();
          // Capture scroll event.
          $window.bind('scroll.wookmark', onScroll);
        })(jQuery);

          $scope.nomark1 = 1;
          $scope.markimg = $scope.currentPage;
      }, 800); 

    },function(data){
      alert(data.data.info);
    });
  };
  $scope.getWorks();

  $scope.gotoDetail = function(id){
    $location.url('bigart/' + id);
  }

  $scope.change_language = function(id){
      $scope.language = id;
      $scope.getWorks();
      var expireDate = new Date();  
    expireDate.setDate(expireDate.getDate() + 30);                
    $cookies.put('language', id, {'expires': expireDate.toUTCString()});
    location.reload();
    if(id == 0){
      document.getElementById('title').innerHTML = '提示';
      document.getElementById('windowclosebutton').value = '确定';
    }else{
      document.getElementById('title').innerHTML = 'Hint';
      document.getElementById('windowclosebutton').value = 'Confirm';
    }

  }

  $scope.getLanguage = function(){
    $scope.language = $cookies.get('language');
    if($scope.language){
      $scope.language = $scope.language;
    }else{
      $scope.language = 0;
    }
    if($scope.language == 0){
      document.getElementById('title').innerHTML = '提示';
      document.getElementById('windowclosebutton').value = '确定';
    }else{
      document.getElementById('title').innerHTML = 'Hint';
      document.getElementById('windowclosebutton').value = 'Confirm';
    }

    if($scope.language==0){
      switch($stateParams.categoryId){
        case '10': $scope.viewtitle = '作品列表';break;
        case '11': $scope.viewtitle = '四方当代艺术湖区';break;
        default: $scope.viewtitle = '作品列表';return;
      }
    }else{
      switch($stateParams.categoryId){
        case '10': $scope.viewtitle = 'Artworks';break;
        case '11': $scope.viewtitle = 'Sifang Collective';break;
        default: $scope.viewtitle = 'Artworks';return;
      }
    }
  }
  $scope.getLanguage();


  // (function ($){
  //   var $tiles = $('#tiles2'),
  //       $handler = $('li', $tiles),
  //       $main = $('#main2'),
  //       $window = $(window),
  //       $document = $(document),
  //       options = {
  //         autoResize: true, // This will auto-update the layout when the browser window is resized.
  //         container: $main, // Optional, used for some extra CSS styling
  //         offset: 10, // Optional, the distance between grid items
  //         itemWidth: $scope.width2 // Optional, the width of a grid item
  //       };

  //   /**
  //    * Reinitializes the wookmark handler after all images have loaded
  //    */
  //   function applyLayout() {
  //     $tiles.imagesLoaded(function() {
  //       // Destroy the old handler
  //       if ($handler.wookmarkInstance) {
  //         $handler.wookmarkInstance.clear();
  //       }

  //       // Create a new layout handler.
  //       $handler = $('li', $tiles);
  //       $handler.wookmark(options);
  //     });
  //   }

  //   /**
  //    * When scrolled all the way to the bottom, add more tiles
  //    */
  //   function onScroll() {
  //     // Check if we're within 100 pixels of the bottom edge of the broser window.
  //     var winHeight = window.innerHeight ? window.innerHeight : $window.height(), // iphone fix
  //         closeToBottom = ($window.scrollTop() + winHeight > $document.height() - 100);

  //     if (closeToBottom) {
  //       // Get the first then items from the grid, clone them, and add them to the bottom of the grid
  //       var $items = $('li', $tiles),
  //           $firstTen = $items.slice(0, 10);
  //       $tiles.append($firstTen.clone());

  //       applyLayout();
  //     }
  //   };

  //   // Call the layout function for the first time
  //   applyLayout();

  //   // Capture scroll event.
  //   $window.bind('scroll.wookmark', onScroll);
  // })(jQuery);

  $scope.gotoMap = function(){
    window.open('http://wxb7b438d8ca6c77e5.pro.brtbeacon.com.cn/nav');
  }

  $scope.gotoMap2 = function(){
    var a = 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/map.jpg';
    var tmp = ['http://store.sifangartmuseum.org.cn/Public/museum/www/img/map.jpg'];
    uploadImg.previewImg(a, tmp);
  }

})